package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.WallpaperActivity;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RecommendAdapter recommendAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};
    private Integer[] tabImg = new Integer[]{};


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivTop.setOnClickListener(this);
        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivTop:
                mDataBinding.scrollView.fullScroll(View.FOCUS_UP);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        WallpaperActivity.listWallpaper=recommendAdapter.getData();
        WallpaperActivity.selPosition=position;
        startActivity(WallpaperActivity.class);
    }

    @Override
    protected void initData() {
        tabTitle = new String[]{getString(R.string.wallpaper_type1), getString(R.string.wallpaper_type2), getString(R.string.wallpaper_type3), getString(R.string.wallpaper_type4), getString(R.string.wallpaper_type5), getString(R.string.wallpaper_type6)};
        tabImg = new Integer[]{R.drawable.atuijian, R.drawable.adanzaipd, R.drawable.amengshoupd, R.drawable.axiangchangpd, R.drawable.ahunaopaid, R.drawable.acaijipd};
        getData();
        setTabData();
    }

    private void getData() {
        //获取数据
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkApi.createParamMap(0, 10), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                recommendAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void setTabData() {
        List<String> list = new ArrayList<>();
        list.add(AppConfig.WALLPAPER_DATA1);
        list.add(AppConfig.WALLPAPER_DATA2);
        list.add(AppConfig.WALLPAPER_DATA3);
        list.add(AppConfig.WALLPAPER_DATA4);
        list.add(AppConfig.WALLPAPER_DATA5);
        list.add(AppConfig.WALLPAPER_DATA6);
        for (int i = 0; i < list.size(); i++) {
            fragments.add(new TabFragment().newInstance(list.get(i)));
            mDataBinding.tlWallpaper.addTab(mDataBinding.tlWallpaper.newTab().setText(tabTitle[i]));
        }
        mDataBinding.tlWallpaper.setTabMode(TabLayout.MODE_SCROLLABLE);
        mDataBinding.tlWallpaper.setSelectedTabIndicatorColor(Color.TRANSPARENT);
        mDataBinding.tlWallpaper.setFocusableInTouchMode(false);
//        mDataBinding.vpWallpaper.setOffscreenPageLimit(fragments.size());
        mDataBinding.vpWallpaper.initIndexList(fragments.size());
        mDataBinding.vpWallpaper.setAdapter(new ArticleAdapter(getChildFragmentManager()));
        mDataBinding.tlWallpaper.setupWithViewPager(mDataBinding.vpWallpaper);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlWallpaper.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlWallpaper.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlWallpaper.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTab);
        ImageView iv = view.findViewById(R.id.ivTab);
        iv.setVisibility(View.VISIBLE);
        tv.setVisibility(View.GONE);
        //设置TabLayout点击事件
        mDataBinding.tlWallpaper.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                mDataBinding.vpWallpaper.setCurrentItem(tab.getPosition());
//                mDataBinding.vpWallpaper.updateHeight(tab.getPosition());
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.VISIBLE);
                tv.setVisibility(View.GONE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mDataBinding.vpWallpaper.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //在每次切换的时候更新高度
                mDataBinding.vpWallpaper.updateHeight(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab, null);
        TextView text = view.findViewById(R.id.tvTab);
        ImageView img = view.findViewById(R.id.ivTab);
        text.setText(tabTitle[i]);
        img.setImageResource(tabImg[i]);
        img.setVisibility(View.GONE);
        return view;
    }

}